<?php
/**
 * Description of TSqlSelectclass
 *
 * @author Saulo
 */
class TSqlSelect extends TSqlInstruction {
    private $columns;

    /*
     * metodo addColumn
     * adiciona uma coluna a ser retornada pelo SELECT
     * @param $columns = coluna da tabela
     */
    public function addColumn($column)
    {
        $this->columns[] = $column;
    }

    public function getInstruction()
    {
        $this->sql = 'SELECT ';
        if(!$this->columns){
            $this->sql .= '*';
        }else{
            $this->sql .= implode(',',$this->columns);
        }
        $this->sql .= ' FROM '.$this->entity;
        //obtem a clausula where do objeto criteria
        if($this->criteria)
        {
            $expression = $this->criteria->dump();
            if($expression)
            {
                $this->sql .= ' WHERE '.$expression;
            }
            $order = $this->criteria->getProperty('order');
            $limit = $this->criteria->getProperty('limit');
            $offset = $this->criteria->getProperty('offset');
            //obtem a ordenação do SELECT
            if($order)
            {
                $this->sql .=' ORDER BY '.$order;
            }
            if($limit)
            {
                $this->sql .=' LIMIT '.$limit;
            }
            if($offset)
            {
                $this->sql .=' OFFSET '.$offset;
            }
        }
        return $this->sql;
    }
}
?>
